// Copyright Notice:
//				google.js v1.0.2
//			Copyright@2008 - OpenSiteMobile
//				All rights reserved
// ==========================================================================
//			http://www.opensitemobile.com
// ==========================================================================
// Contact Information:
//			Author: Dwight Vietzke
//			Email:  dwight_vietzke@yahoo.com

/*
	OpenSiteMobile 'Google' functions w/dojo integration
*/

dojo.provide("msos.google");
dojo.require("msos.common");


// --------------------------
// Google AdSense Manipulation
// --------------------------

msos.google.ad_display_tool = function (display_elm) {
	this.adj_pos_top = 1;
	this.adj_pos_left = 3;
	this.delay_ad_fade = 3000;	// initial ad display delay
	this.delay_ad_rotate = 20000;	// ad rotation interval
	this.fade_duration = 500;	// fade duration in milliseconds
	this.rotate_multiple = 1.3;	// delay increase factor
	this._display_html = [];
	this._timeout_ref = null;
	this._index = 0;

	var ads = this;

	// Once clicked, stop all javascript processing
	dojo.connect(display_elm, "onclick", null,
		function (evt) {
			evt.preventDefault();
			evt.stopPropagation();
			if (ads._timeout_ref) { clearTimeout(ads._timeout_ref); }
			msos.common.event_debug(evt, "\ngoogle_ad_display_tool:\nCleared timeouts");
		}
	);

	// Rotate ad(s): Fade in and Fade out our Google ad(s)
	fade_in_out_func = function() {

		// Set the next index to use
		ads._index = ads._display_html[ads._index + 1] ? ads._index + 1 : 0;

		var fade_in_vars = {
			node: display_elm,
			duration: ads.fade_duration // milliseconds until fade
		};

		var fade_in = function() { dojo.fadeIn(fade_in_vars).play(); }

		var fade_out_vars = {
			node: display_elm,
			duration: ads.fade_duration,
			onEnd: function() {
				display_elm.innerHTML = ads._display_html[ads._index];
				setTimeout(fade_in, ads.fade_duration + 100);
			}
		};

		// Note: This could have been done with dojo.fx.chain, but that requires dojo.fx.
		// 	 So instead we use the above 'hand rolled' version which only needs base dojo
		
		dojo.fadeOut(fade_out_vars).play();

		// And continue to run until the user clicks on the display element
		ads._timeout_ref = setTimeout(fade_in_out_func, ads.delay_ad_rotate);

		// After full rotation, increase rotation interval
		if (ads._index == 0) ads.delay_ad_rotate *= ads.rotate_multiple;
	}

	// This checks our Google iframe(s) html and sets the process in motion
	this.start_fade_in_out = function() {

		var iframes = dojo.query('iframe') || [];
		// Test to make sure we have only Google AdSense iframes
		var src_pattern_1 = new RegExp('google_ads_frame');
		var src_pattern_2 = new RegExp('googleads.g.doubleclick');
		for(var i = 0; i < iframes.length; i++) {
			if (src_pattern_1.test(iframes[i].id) || src_pattern_2.test(iframes[i].src)) {
				// Now we have the iframe, so set the position correct for our display
				msos.common.set_popup_position(iframes[i], display_elm, { adj_left: ads.adj_pos_left, adj_top: ads.adj_pos_top });
				var ifr_parent = iframes[i].parentNode;
				var ifr_html = ifr_parent.innerHTML;
				// I only want the iframe part...
				var indx = ifr_html.indexOf("<iframe");
				ifr_html = ifr_html.substring(indx);
				ads._display_html.push(ifr_html);
			}
		}

		if (djConfig.msos_content == 'xml') {
			console.warn('google_ad_display_tool - Not available (as of 2/13/09) in an xml page!');
		}
		if (ads._display_html.length > 0) {
			var default_html = display_elm.innerHTML;
			// Position default as first array element
			ads._display_html.unshift(default_html);
			console.debug('google_ad_display_tool -> found ' + (ads._display_html.length - 1) + ' google iframes');
			ads._timeout_ref = setTimeout(fade_in_out_func, ads.delay_ad_fade);
		} else {
			console.warn('google_ad_display_tool - Found no iframes');
		  }
	}
}